package dao.impl;

import dao.UserDao;
import pojo.User;
import service.Userservice;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class UserDaoimpl implements UserDao {
    public List<User> findAll(){
        //TODO


        List<User> list=new ArrayList<>();
        String url="jdbc:mysql://localhost:3306/jdbc";
        String user="root";
        String password="050810";
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        try {


            Class.forName("com.mysql.cj.jdbc.Driver");
            conn= DriverManager.getConnection(url,user,password);

            String sql="SELECT  * FROM user1";
            pstmt=conn.prepareStatement(sql);

            rs=pstmt.executeQuery();
            while(rs.next()){
                User u=new User();
                u.setId(rs.getInt("id"));
                u.setName(rs.getString("name"));
                u.setPassword(rs.getString("pass"));
                list.add(u);

            }

            return list;

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            try{
                if(rs!=null) rs.close();
                if(pstmt!=null) pstmt.close();
                if(conn!=null) conn.close();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
